/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseRolePermissionScopeDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseRolePermissionScopeDao : BaseDao, IBaseDao
    {
        public BaseRolePermissionScopeDao()
        {
            base.CurrentTableName = BaseResourcePermissionScopeTable.TableName;
        }

        public BaseRolePermissionScopeDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseRolePermissionScopeDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseRolePermissionScopeDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string[] GetLicensePermissionIDs(string roleID)
        {
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceID;
            values[1] = roleID;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Permission";
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionScopeTable.FieldTargetID);
        }

        public string[] GetModuleIDs(string resourceID, string permissionID)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionID;
            values[0] = permissionID;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceID;
            values[2] = resourceID;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Module";
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionScopeTable.FieldTargetID);
        }

        public string[] GetOrganizeIDs(string roleID, string permissionID)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionID;
            values[0] = permissionID;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceID;
            values[2] = roleID;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Organize";
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionScopeTable.FieldTargetID);
        }

        public string[] GetRoleIDs(string roleID, string permissionID)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionID;
            values[0] = permissionID;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceID;
            values[2] = roleID;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Role";
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionScopeTable.FieldTargetID);
        }

        public string[] GetUserIDs(string roleID, string permissionID)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionID;
            values[0] = permissionID;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceID;
            values[2] = roleID;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "User";
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionScopeTable.FieldTargetID);
        }

        public int GrantLicense(string[] roleIDs, string permissionID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                this.GrantLicense(resourcePermissionScopeDao, strArray[i], roleIDs[i], permissionID);
                num++;
            }
            return num;
        }

        public int GrantLicense(string[] roleIDs, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length * permissionIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < permissionIDs.Length; j++)
                {
                    this.GrantLicense(resourcePermissionScopeDao, strArray[(i * permissionIDs.Length) + j], roleIDs[i], permissionIDs[j]);
                    num++;
                }
            }
            return num;
        }

        public string GrantLicense(string roleID, string permissionID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.GrantLicense(resourcePermissionScopeDao, string.Empty, roleID, permissionID);
        }

        public int GrantLicense(string roleID, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, permissionIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                this.GrantLicense(resourcePermissionScopeDao, strArray[i], roleID, permissionIDs[i]);
                num++;
            }
            return num;
        }

        private string GrantLicense(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string pramID, string roleID, string permissionID)
        {
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity
            {
                ID = pramID,
                PermissionID = "License",
                ResourceCategory = "Role",
                ResourceID = roleID,
                TargetCategory = "Permission",
                TargetID = permissionID
            };
            return resourcePermissionScopeDao.Add(resourcePermissionScope);
        }

        public string GrantModule(string roleID, string permissionID, string targetModuleID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.GrantModule(resourcePermissionScopeDao, string.Empty, roleID, permissionID, targetModuleID);
        }

        public int GrantModule(string roleID, string permissionID, string[] targetModuleIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, targetModuleIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < targetModuleIDs.Length; i++)
            {
                this.GrantModule(resourcePermissionScopeDao, strArray[i], roleID, permissionID, targetModuleIDs[i]);
                num++;
            }
            return num;
        }

        public int GrantModule(string[] roleIDs, string permissionID, string[] targetModuleIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length * targetModuleIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < targetModuleIDs.Length; j++)
                {
                    this.GrantModule(resourcePermissionScopeDao, strArray[(i * roleIDs.Length) + j], roleIDs[i], permissionID, targetModuleIDs[j]);
                    num++;
                }
            }
            return num;
        }

        public int GrantModule(string[] roleIDs, string permissionID, string targetModuleID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                this.GrantModule(resourcePermissionScopeDao, strArray[i], roleIDs[i], permissionID, targetModuleID);
                num++;
            }
            return num;
        }

        private string GrantModule(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string pramID, string roleID, string permissionID, string targetModuleID)
        {
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity
            {
                ID = pramID,
                PermissionID = permissionID,
                ResourceCategory = "Role",
                ResourceID = roleID,
                TargetCategory = "Module",
                TargetID = targetModuleID
            };
            return resourcePermissionScopeDao.Add(resourcePermissionScope);
        }

        public int GrantOrganize(string roleID, string permissionID, string[] organizeIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, organizeIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < organizeIDs.Length; i++)
            {
                this.GrantOrganize(resourcePermissionScopeDao, strArray[i], roleID, permissionID, organizeIDs[i]);
                num++;
            }
            return num;
        }

        public int GrantOrganize(string[] roleIDs, string permissionID, string organizeID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                this.GrantOrganize(resourcePermissionScopeDao, strArray[i], roleIDs[i], permissionID, organizeID);
                num++;
            }
            return num;
        }

        public string GrantOrganize(string roleID, string permissionID, string organizeID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.GrantOrganize(resourcePermissionScopeDao, string.Empty, roleID, permissionID, organizeID);
        }

        public int GrantOrganize(string[] roleIDs, string permissionID, string[] organizeIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, roleIDs.Length * organizeIDs.Length);
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < organizeIDs.Length; j++)
                {
                    this.GrantOrganize(resourcePermissionScopeDao, strArray[(i * organizeIDs.Length) + j], roleIDs[i], permissionID, organizeIDs[j]);
                    num++;
                }
            }
            return num;
        }

        private string GrantOrganize(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string pramID, string roleID, string permissionID, string organizeID)
        {
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity
            {
                ID = pramID,
                PermissionID = permissionID,
                ResourceCategory = "Role",
                ResourceID = roleID,
                TargetCategory = "Organize",
                TargetID = organizeID
            };
            return resourcePermissionScopeDao.Add(resourcePermissionScope);
        }

        public int RevokeLicense(string[] roleIDs, string permissionID)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                this.RevokeLicense(resourcePermissionScopeDao, roleIDs[i], permissionID);
                num++;
            }
            return num;
        }

        public int RevokeLicense(string roleID, string[] permissionIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                this.RevokeLicense(resourcePermissionScopeDao, roleID, permissionIDs[i]);
                num++;
            }
            return num;
        }

        public int RevokeLicense(string roleID, string permissionID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.RevokeLicense(resourcePermissionScopeDao, roleID, permissionID);
        }

        public int RevokeLicense(string[] roleIDs, string[] permissionIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < permissionIDs.Length; j++)
                {
                    this.RevokeLicense(resourcePermissionScopeDao, roleIDs[i], permissionIDs[j]);
                    num++;
                }
            }
            return num;
        }

        private int RevokeLicense(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string roleID, string permissionID)
        {
            string[] names = new string[4];
            string[] strArray2 = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            strArray2[0] = "Role";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceID;
            strArray2[1] = roleID;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            strArray2[2] = "Permission";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetID;
            strArray2[3] = permissionID;
            return resourcePermissionScopeDao.BatchDelete(names, (object[])strArray2);
        }

        public int RevokeModule(string roleID, string permissionID, string[] targetModuleIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < targetModuleIDs.Length; i++)
            {
                num += this.RevokeModule(resourcePermissionScopeDao, roleID, permissionID, targetModuleIDs[i]);
            }
            return num;
        }

        public int RevokeModule(string roleID, string permissionID, string targetModuleID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.RevokeModule(resourcePermissionScopeDao, permissionID, roleID, targetModuleID);
        }

        public int RevokeModule(string[] roleIDs, string permissionID, string[] targetModuleIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < targetModuleIDs.Length; j++)
                {
                    num += this.RevokeModule(resourcePermissionScopeDao, roleIDs[i], permissionID, targetModuleIDs[j]);
                }
            }
            return num;
        }

        public int RevokeModule(string[] roleIDs, string permissionID, string targetModuleID)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                num += this.RevokeModule(resourcePermissionScopeDao, roleIDs[i], permissionID, targetModuleID);
            }
            return num;
        }

        private int RevokeModule(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string roleID, string permissionID, string targetModuleID)
        {
            string[] names = new string[4];
            string[] strArray2 = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            strArray2[0] = "Role";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceID;
            strArray2[1] = roleID;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            strArray2[2] = "Module";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetID;
            strArray2[3] = targetModuleID;
            return resourcePermissionScopeDao.BatchDelete(names, (object[])strArray2);
        }

        public int RevokeOrganize(string roleID, string permissionID, string organizeID)
        {
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            return this.RevokeOrganize(resourcePermissionScopeDao, permissionID, roleID, organizeID);
        }

        public int RevokeOrganize(string roleID, string permissionID, string[] organizeIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < organizeIDs.Length; i++)
            {
                num += this.RevokeOrganize(resourcePermissionScopeDao, roleID, permissionID, organizeIDs[i]);
            }
            return num;
        }

        public int RevokeOrganize(string[] roleIDs, string permissionID, string[] organizeIDs)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                for (int j = 0; j < organizeIDs.Length; j++)
                {
                    num += this.RevokeOrganize(resourcePermissionScopeDao, roleIDs[i], permissionID, organizeIDs[j]);
                }
            }
            return num;
        }

        public int RevokeOrganize(string[] roleIDs, string permissionID, string organizeID)
        {
            int num = 0;
            BaseResourcePermissionScopeDao resourcePermissionScopeDao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                num += this.RevokeOrganize(resourcePermissionScopeDao, roleIDs[i], permissionID, organizeID);
            }
            return num;
        }

        private int RevokeOrganize(BaseResourcePermissionScopeDao resourcePermissionScopeDao, string roleID, string permissionID, string organizeID)
        {
            string[] names = new string[4];
            string[] strArray2 = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            strArray2[0] = "Role";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceID;
            strArray2[1] = roleID;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            strArray2[2] = "Organize";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetID;
            strArray2[3] = organizeID;
            return resourcePermissionScopeDao.BatchDelete(names, (object[])strArray2);
        }
    }
}